﻿// 项目：	Guan.CMS.Domain - Guan.CMS
// 文件名：	UserRegisterHandler.cs
// 
// 修改时间：2013-03-08 22:05
// 修改人：	Cooclsee

using System;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Data.Infrastructure;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.Account;
using Guan.CMS.Model.Entities;

namespace Guan.CMS.Domain.Handlers.Account
{
    public class UserRegisterHandler : ICommandHandler<UserRegisterCommand>
    {
        private readonly IUserRepository _userRepository;
        private readonly IUnitOfWork _unitOfWork;

        public UserRegisterHandler(IUserRepository userRepository, IUnitOfWork unitOfWork)
        {
            this._userRepository = userRepository;
            this._unitOfWork = unitOfWork;
        }

        public ICommandResult Execute(UserRegisterCommand command)
        {
            var user = new User
            {
                Name = command.Name,
                UserName = command.UserName,
                Email = command.Email,
                Password = command.Password,
                RoleId = command.RoleId,
                DateCreated = DateTime.Now,
                LastLoginTime = DateTime.Now,
                Activated = command.Activated
            };
            this._userRepository.Add(user);
            this._unitOfWork.Commit();
            return new CommandResult(true);
        }
    }
}